import java.util.Scanner;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
//111
public class PasswordManager {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int choice;
//完成以下所有功能


//增加3，判断密码强度和4.生成密码功能


        do {
            System.out.println("---------------------");
            System.out.println("欢迎使用密码管理系统");
            System.out.println("---------------------");
            System.out.println("请选择功能：");
            System.out.println("1. 加密");
            System.out.println("2. 解密");
            System.out.println("3. 判断密码强度");
            System.out.println("4. 生成密码");
            System.out.println("5. 退出");
            choice = scanner.nextInt();
            scanner.nextLine();  // 消耗掉换行符

            switch (choice) {
                case 1:
                    System.out.println("请输入要加密的密码：");
                    String password = scanner.nextLine();
                    String encryptedPassword = encryptPassword(password);
                    System.out.println("加密后的密码为：" + encryptedPassword);
                    break;
                case 2:
                    System.out.println("请输入要解密的密码：");
                    String encryptedPasswordToDecrypt = scanner.nextLine();
                    String decryptedPassword = decryptPassword(encryptedPasswordToDecrypt);
                    System.out.println("解密后的密码为：" + decryptedPassword);
                    break;
                case 3:
                    System.out.println("请输入要检查的密码：");
                    String passwordToCheck = scanner.nextLine();
                    String strength = checkPasswordStrength(passwordToCheck);
                    System.out.println("密码强度：" + strength);
                    break;
                case 4:
                    System.out.println("请输入要生成的密码长度：");
                    int passwordLength = scanner.nextInt();
                    scanner.nextLine();  // 消耗掉换行符
                    String generatedPassword = generatePassword(passwordLength);
                    System.out.println("生成的密码为：" + generatedPassword);
                    break;
                case 5:
                    System.out.println("再见！");
                    break;
                default:
                    System.out.println("无效的选择！");
            }
        } while (choice != 5);

        scanner.close();
    }

    public static String encryptPassword(String password) {
        // 加密逻辑
        // ...

            StringBuilder encryptedPassword = new StringBuilder();
            int length = password.length();

            for (int i = 0; i < length; i++) {
                char c = password.charAt(i);
                int ascii = (int) c + i + 3;
                encryptedPassword.append((char) ascii);
            }
            //交换字符串的第一个字符和最后一个字符
            encryptedPassword = swapFirstAndLast(encryptedPassword);
            //反转整个加密后地字符串
            encryptedPassword = encryptedPassword.reverse();

            return encryptedPassword.toString();
        }


    public static String decryptPassword(String encryptedPassword) {
        // 解密逻辑
        // ...

            StringBuilder decryptedPassword = new StringBuilder(encryptedPassword).reverse();
            decryptedPassword = swapFirstAndLast(decryptedPassword);

            int length = decryptedPassword.length();
            for (int i = 0; i < length; i++) {
                char c = decryptedPassword.charAt(i);
                //对每个字符串进行解密，将ASCII值减去当前字符的索引和3
                int ascii = (int) c - i - 3;
                decryptedPassword.setCharAt(i, (char) ascii);
            }

            return decryptedPassword.toString();
        }
    // 交换字符串的第一个字符和最后一个字符
    public static StringBuilder swapFirstAndLast(StringBuilder str) {
        int length = str.length();
        if (length >= 2) {
            char firstChar = str.charAt(0);
            char lastChar = str.charAt(length - 1);
            str.setCharAt(0, lastChar);
            str.setCharAt(length - 1, firstChar);
        }
        return str;
    }

    public static String checkPasswordStrength(String password) {
        int length = password.length();
        if (length < 8) {
            return "弱强度：密码长度小于8个字符";
        }

        boolean hasDigit = false;
        boolean hasLowercase = false;
        boolean hasUppercase = false;

        for (char c : password.toCharArray()) {
            if (Character.isDigit(c)) {
                hasDigit = true;
            } else if (Character.isLowerCase(c)) {
                hasLowercase = true;
            } else if (Character.isUpperCase(c)) {
                hasUppercase = true;
            }
        }

        if (hasDigit && hasLowercase && hasUppercase) {
            return "高强度：密码包含数字、小写字母和大写字母";
        } else if (hasDigit || hasLowercase || hasUppercase) {
            return "中强度：密码包含数字和/或字母";
        } else {
            return "弱强度：密码只包含一种字符类型";
        }
    }

    public static String generatePassword(int length) {
        String characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        StringBuilder generatedPassword = new StringBuilder();

        Random random = new Random();
        for (int i = 0; i < length; i++) {
            int index = random.nextInt(characters.length());
            generatedPassword.append(characters.charAt(index));
        }

        return generatedPassword.toString();
    }
}

